package com.dc.dbus.netty;

import com.dc.dbus.session.listener.ServerSideEventListener;
import com.dc.dbus.session.register.ServiceRegister;
import com.dc.dbus.session.register.ZookeeperServiceRegister;
import com.dc.dbus.session.server.Server;
import com.dc.dbus.transport.commons.ServerAddress;
import com.dc.dbus.transport.commons.ServerSideTransport;
import com.dc.dbus.transport.netty.server.NettyServerSideTransport;

public class NettyServer {

	protected final static ServerSideEventListener serverSideEventListener = new ServerSideEventListener() {
		@Override
		public void queueFull(String message, Exception e) {
			System.err.println(message);
			e.printStackTrace(System.err);
		}

		@Override
		public void methodExcuteException(String message, Throwable t) {
			System.err.println(message);
			t.printStackTrace(System.err);
		}

		@Override
		public void dbusException(String message, Exception e) {
			System.err.println(message);
			e.printStackTrace(System.err);
		}
	};

	public static void main(String[] args) throws Exception {

		ServerAddress serverAddress = new ServerAddress(9934);

		ServiceRegister serviceRegister = ZookeeperServiceRegister.getServerSideReiger("127.0.0.1:2181", serverAddress);

		ServerSideTransport serverSideTansport = new NettyServerSideTransport(serverAddress);

		Server server = new Server(serviceRegister, serverSideTansport, null, serverSideEventListener);

		ITestService test = new ITestService() {
			@Override
			public int test3(String router, String p1) {
				System.out.println("get req: test3:" + p1);
				return 666;
			}

			@Override
			public Integer test2(String router, String p1) {
				System.out.println("get req: test2:" + p1);
				return 666666;
			}

			@Override
			public String test1(String router, String p1) {
				System.out.println("get req: test1:" + p1);
				return "6";
			}
		};

		server.registerProducter(ITestService.class.getName(), new String[] {"ts0", "ts1"},
				ITestService.class, test);

	}
}
